﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Athanor.Gui
{
    public class GuiManager
    {
        # region Singleton 

        private static GuiManager sceneManager;

        public static GuiManager GetInstance()
        {
            if (sceneManager == null)
                sceneManager = new GuiManager();
            return sceneManager;
        }

        # endregion

        private GuiManager()
        {
            effects = new List<GuiEffect>();
            screens = new Dictionary<string, Sprite>();
            spriteBatch = new SpriteBatch(ResourcesManager.GetInstance().GetGraphicsDeviceManager().GraphicsDevice);
        }

        private List<GuiEffect> effects;
        private Dictionary<string, Sprite> screens;
        private SpriteBatch spriteBatch;

        public void Init()
        {
            TextComponent.defaultFontName = "AthanorFont";
        }

        public void Update(GameTime gameTime)
        {
            foreach (GuiEffect effect in effects)
                effect.Update(gameTime);

            foreach (Sprite screen in screens.Values)
                screen.Update(gameTime);
        }

        public void Draw(GameTime gameTime)
        {
//            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
            spriteBatch.Begin();

            foreach (Sprite screen in screens.Values)
                screen.Draw(Vector2.Zero, Vector2.One);

            spriteBatch.End();
        }

        public void AddComponent(string name, Sprite comp)
        {
            screens[name] = comp;
        }

        public void RemoveComponent(string name)
        {
            screens.Remove(name);
        }

        public Sprite GetSprite(string name){
            return screens[name];
        }

        public void AddEffect(GuiEffect effect)
        {
            effects.Add(effect);
        }

        public void RemoveEffect(GuiEffect effect)
        {
            effects.Remove(effect);
        }

        public SpriteBatch GetSpriteBatch() { return spriteBatch; }
    }
}
